import { math, EventTarget } from "cc"
import { UserPreference } from "./UserPreference"

export class Setting extends EventTarget {
    private static _instance: Setting = null
    public static get instance() {
        if (!this._instance) this._instance = new Setting()
        return this._instance
    }
    private _bgmVolume = 1
    private _sfxVolume = 1
    set bgmVolume(value: number) {
        this._bgmVolume = math.clamp01(this._bgmVolume)
        UserPreference.setFloat("bgmVolume", this._bgmVolume) //记录本地

        //发送事件
        this.emit("onBgmVolumeChange", this._bgmVolume)
    }
    get bgmVolume(): number {
        return this._bgmVolume
    }

    set sfxVolume(value: number) {
        this._sfxVolume = math.clamp01(this._sfxVolume)
        UserPreference.setFloat("sfxVolume", this._sfxVolume) //记录本地

        //发送事件
        // this.emit()
    }
    get sfxVolume(): number {
        return this._sfxVolume
    }

    load() {
        this._bgmVolume = UserPreference.getFloat("bgmVolume")
        this._sfxVolume = UserPreference.getFloat("sfxVolume")
    }

}